<template><div><h2 id="数据库迁移-database-migrations" tabindex="-1"><a class="header-anchor" href="#数据库迁移-database-migrations"><span>数据库迁移（Database Migrations）</span></a></h2>
<p>在开发和维护一个数据库驱动的应用程序时，数据库的结构会像代码一样不断演变。例如，在开发应用程序的过程中，会增加一张新表且必须得加进来；在应用程序被部署到生产环境后，需要建立一个索引来提高查询的性能等等。因为一个数据库结构发生改变的时候源代码也经常会需要做出改变，Yii 提供了一个 <em>数据库迁移</em> 功能，该功能可以记录数据库的变化，以便使数据库和源代码一起受版本控制。</p>
<p>如下的步骤向我们展示了数据库迁移工具是如何为开发团队所使用的：</p>
<ol>
<li>Tim 创建了一个新的迁移对象（例如，创建一张新表，改变字段的定义等）。</li>
<li>Tim 将这个新的迁移对象提交到代码管理系统（例如，Git，Mercurial）。</li>
<li>Doug 从代码管理系统当中更新版本并获取到这个新的迁移对象。</li>
<li>Doug 把这个迁移对象提交到本地的开发数据库当中，这样，Doug 就同步了 Tim 所做的修改。</li>
</ol>
<p>如下的步骤向我们展示了，如何发布一个附带数据库迁移的新版本到生产环境当中：</p>
<ol>
<li>Scott 为一个包含数据库迁移的项目版本创建了一个发布标签。</li>
<li>Scott 把发布标签的源代码更新到生产环境的服务器上。</li>
<li>Scott 把所有的增量数据库迁移提交到生产环境数据库当中。</li>
</ol>
<p>Yii 提供了一整套的迁移命令行工具，通过这些工具你可以：</p>
<ul>
<li>创建新的迁移；</li>
<li>提交迁移；</li>
<li>恢复迁移；</li>
<li>重新提交迁移；</li>
<li>展示迁移历史和状态。</li>
</ul>
<p>所有的这些工具都可以通过 <code v-pre>yii migrate</code> 命令来进行操作。在这一章节，我们将详细的介绍如何使用这些工具来完成各种各样的任务。你也可以通过 <code v-pre>yii help migrate</code> 命令来获取每一种工具的具体使用方法。</p>
<blockquote>
<p>Tip: 迁移不仅仅只作用于数据库表，它同样会调整现有的数据来适应新的表单、创建 RBAC 分层、又或者是清除缓存。</p>
</blockquote>
<blockquote>
<p>Note: 当你使用迁移来操作数据时，你会发现使用 <a href="https://github.com/yiisoft/yii2/blob/master/docs/guide-zh-CN/db-active-record.md" target="_blank" rel="noopener noreferrer">活动记录</a> 类很有帮助，因为一些逻辑已经在那里实现了。然而别忘了，相比于天生保持恒定不变的迁移类中的代码，程序逻辑是随时变化的。所以当你在迁移中使用活动记录类时，活动记录层中逻辑的变化可能会意外打断现有的迁移。基于这个原因，迁移代码应该保持独立于其他程序逻辑，比如活动记录类。</p>
</blockquote>
<h2 id="创建迁移" tabindex="-1"><a class="header-anchor" href="#创建迁移"><span>创建迁移</span></a></h2>
<p>使用如下命令来创建一个新的迁移：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">yii migrate/create <span class="token operator">&lt;</span>name<span class="token operator">></span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>必填参数 <code v-pre>name</code> 的作用是对新的迁移做一个简要的描述。例如，如果这个迁移是用来创建一个叫做 <em>news</em> 的表，那么你可以使用 <code v-pre>create_news_table</code> 这个名称并运行如下命令：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">yii migrate/create create_news_table</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><blockquote>
<p>Note: 因为 <code v-pre>name</code> 参数会被用来生成迁移的类名的一部分，所以该参数应当只包含字母、数字和下划线。</p>
</blockquote>
<p>如上命令将会在 <code v-pre>@app/migrations</code> 目录下创建一个新的名为 <code v-pre>m150101_185401_create_news_table.php</code> 的 PHP 类文件。该文件包含如下的代码，它们用来声明一个迁移类 <code v-pre>m150101_185401_create_news_table</code>，并附有代码框架：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token php language-php"><span class="token delimiter important">&lt;?php</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">yii<span class="token punctuation">\</span>db<span class="token punctuation">\</span>Migration</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">class</span> <span class="token class-name-definition class-name">m150101_185401_create_news_table</span> <span class="token keyword">extends</span> <span class="token class-name">Migration</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">up</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line"></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">down</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token keyword">echo</span> <span class="token string double-quoted-string">"m101129_185401_create_news_table cannot be reverted.\n"</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">        <span class="token keyword">return</span> <span class="token constant boolean">false</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">/*</span>
<span class="line">    // Use safeUp/safeDown to run migration code within a transaction</span>
<span class="line">    public function safeUp()</span>
<span class="line">    {</span>
<span class="line">    }</span>
<span class="line"></span>
<span class="line">    public function safeDown()</span>
<span class="line">    {</span>
<span class="line">    }</span>
<span class="line">    */</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>每个数据库迁移都会被定义为一个继承自 [[yii\db\Migration]] 的 PHP 类。类的名称按照 <code v-pre>m&lt;YYMMDD_HHMMSS&gt;_&lt;Name&gt;</code> 的格式自动生成，其中</p>
<ul>
<li><code v-pre>&lt;YYMMDD_HHMMSS&gt;</code> 指执行创建迁移命令的 UTC 时间。</li>
<li><code v-pre>&lt;Name&gt;</code> 和你执行命令时所带的 <code v-pre>name</code> 参数值相同。</li>
</ul>
<p>在迁移类当中，你应当在 <code v-pre>up()</code> 方法中编写改变数据库结构的代码。你可能还需要在 <code v-pre>down()</code> 方法中编写代码来恢复由 <code v-pre>up()</code> 方法所做的改变。当你通过 migration 升级数据库时，<code v-pre>up()</code> 方法将会被调用，反之，<code v-pre>down()</code> 将会被调用。如下代码展示了如何通过迁移类来创建一张 <code v-pre>news</code> 表：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">yii<span class="token punctuation">\</span>db<span class="token punctuation">\</span>Schema</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">yii<span class="token punctuation">\</span>db<span class="token punctuation">\</span>Migration</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">class</span> <span class="token class-name-definition class-name">m150101_185401_create_news_table</span> <span class="token keyword">extends</span> <span class="token class-name class-name-fully-qualified"><span class="token punctuation">\</span>yii<span class="token punctuation">\</span>db<span class="token punctuation">\</span>Migration</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">up</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">createTable</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'news'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span></span>
<span class="line">            <span class="token string single-quoted-string">'id'</span> <span class="token operator">=></span> <span class="token class-name static-context">Schema</span><span class="token operator">::</span><span class="token constant">TYPE_PK</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token string single-quoted-string">'title'</span> <span class="token operator">=></span> <span class="token class-name static-context">Schema</span><span class="token operator">::</span><span class="token constant">TYPE_STRING</span> <span class="token operator">.</span> <span class="token string single-quoted-string">' NOT NULL'</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token string single-quoted-string">'content'</span> <span class="token operator">=></span> <span class="token class-name static-context">Schema</span><span class="token operator">::</span><span class="token constant">TYPE_TEXT</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">down</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">dropTable</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'news'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><blockquote>
<p>Note: 并不是所有迁移都是可恢复的。例如，如果 <code v-pre>up()</code> 方法删除了表中的一行数据，这将无法通过 <code v-pre>down()</code> 方法来恢复这条数据。有时候，你也许只是懒得去执行 <code v-pre>down()</code> 方法了，因为它在恢复数据库迁移方面并不是那么的通用。在这种情况下，你应当在 <code v-pre>down()</code> 方法中返回 <code v-pre>false</code> 来表明这个 migration 是无法恢复的。</p>
</blockquote>
<p>migration 的基类 <code v-pre>yii\db\Migration</code> 通过 [[yii\db\Migration::db]] 属性来连接数据库。你可以通过 <a href="https://github.com/yiisoft/yii2/blob/master/docs/guide-zh-CN/db-dao.md#database-schema" target="_blank" rel="noopener noreferrer">操作数据库模式（schema）</a> 章节中所描述的那些方法来操作数据库模式（schema）。</p>
<p>当你创建一张表或者一个字段的时候，你应该使用 <em>抽象类型</em> 而不是 <em>实体类型</em>，这样一来你的迁移对象就可以从特定的 DBMS 当中抽离出来。<code v-pre>yii\db\Schema</code> 类定义了一整套可用的抽象类型常量。这些常量的格式为 <code v-pre>TYPE_&lt;Name&gt;</code>。例如，<code v-pre>TYPE_PK</code> 指代自增主键类型； <code v-pre>TYPE_STRING</code> 指代字符串类型。当迁移对象被提交到某个特定的数据库的时候，这些抽象类型将会被转换成相对应的实体类型。以 MySQL 为例，<code v-pre>TYPE_PK</code> 将会变成 <code v-pre>int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY</code>，而 <code v-pre>TYPE_STRING</code> 则变成 <code v-pre>varchar(255)</code>。</p>
<p>在使用抽象类型的时候，你可以添加额外的约束条件。在上面的例子当中，<code v-pre>NOT NULL</code> 被添加到 <code v-pre>Schema::TYPE_STRING</code> 当中来指定该字段不能为空。</p>
<blockquote>
<p>Tip: 抽象类型和实体类型之间的映射关系是由每个具体的 <code v-pre>QueryBuilder</code> 类当中的 [[yii\db\QueryBuilder::$typeMap]] 属性所指定的。</p>
</blockquote>
<p>从 2.0.6 版本开始，你可以使用能提供更便捷的方法定义字段模式（schema）的新模式（schema）构建器。这样上面的迁移就可以写成这样：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token php language-php"><span class="token delimiter important">&lt;?php</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">yii<span class="token punctuation">\</span>db<span class="token punctuation">\</span>Migration</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">class</span> <span class="token class-name-definition class-name">m150101_185401_create_news_table</span> <span class="token keyword">extends</span> <span class="token class-name">Migration</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">up</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">createTable</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'news'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span></span>
<span class="line">            <span class="token string single-quoted-string">'id'</span> <span class="token operator">=></span> <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">primaryKey</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token string single-quoted-string">'title'</span> <span class="token operator">=></span> <span class="token variable">$this</span><span class="token operator">-></span><span class="token keyword type-declaration">string</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">notNull</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token string single-quoted-string">'content'</span> <span class="token operator">=></span> <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">text</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">down</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">dropTable</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'news'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>所有用来定义字段类型的方法都可以到 <code v-pre>yii\db\SchemaBuilderTrait</code> 的 API 文档中查到。</p>
<h2 id="生成迁移" tabindex="-1"><a class="header-anchor" href="#生成迁移"><span>生成迁移</span></a></h2>
<p>从 2.0.7 版本开始，迁移终端提供了一种创建迁移的便捷方法。</p>
<p>如果迁移对象的名称遵循某种特定的格式，比如 <code v-pre>create_xxx</code> 或者 <code v-pre>drop_xxx</code>，那么生成的迁移代码中将包含创建/删除表的额外代码。在下文中将描述该特型的所有变型。</p>
<h3 id="创建表" tabindex="-1"><a class="header-anchor" href="#创建表"><span>创建表</span></a></h3>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">yii migrate/create create_post</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>生成</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * Handles the creation for table `post`.</span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">class</span> <span class="token class-name-definition class-name">m150811_220037_create_post</span> <span class="token keyword">extends</span> <span class="token class-name">Migration</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token doc-comment comment">/**</span>
<span class="line">     * <span class="token keyword">@inheritdoc</span></span>
<span class="line">     */</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">up</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">createTable</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'post'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span></span>
<span class="line">            <span class="token string single-quoted-string">'id'</span> <span class="token operator">=></span> <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">primaryKey</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line">        <span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"></span>
<span class="line">    <span class="token doc-comment comment">/**</span>
<span class="line">     * <span class="token keyword">@inheritdoc</span></span>
<span class="line">     */</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">down</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">dropTable</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'post'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>利用 <code v-pre>--fields</code> 选项指定字段参数，可以立即创建字段。</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">yii migrate/create create_post <span class="token parameter variable">--fields</span><span class="token operator">=</span><span class="token string">"title:string,body:text"</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>生成</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * Handles the creation for table `post`.</span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">class</span> <span class="token class-name-definition class-name">m150811_220037_create_post</span> <span class="token keyword">extends</span> <span class="token class-name">Migration</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token doc-comment comment">/**</span>
<span class="line">     * <span class="token keyword">@inheritdoc</span></span>
<span class="line">     */</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">up</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">createTable</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'post'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span></span>
<span class="line">            <span class="token string single-quoted-string">'id'</span> <span class="token operator">=></span> <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">primaryKey</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token string single-quoted-string">'title'</span> <span class="token operator">=></span> <span class="token variable">$this</span><span class="token operator">-></span><span class="token keyword type-declaration">string</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token string single-quoted-string">'body'</span> <span class="token operator">=></span> <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">text</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"></span>
<span class="line">    <span class="token doc-comment comment">/**</span>
<span class="line">     * <span class="token keyword">@inheritdoc</span></span>
<span class="line">     */</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">down</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">dropTable</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'post'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>你可以指定更多的字段参数。</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">yii migrate/create create_post <span class="token parameter variable">--fields</span><span class="token operator">=</span><span class="token string">"title:string(12):notNull:unique,body:text"</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>生成</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * Handles the creation for table `post`.</span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">class</span> <span class="token class-name-definition class-name">m150811_220037_create_post</span> <span class="token keyword">extends</span> <span class="token class-name">Migration</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token doc-comment comment">/**</span>
<span class="line">     * <span class="token keyword">@inheritdoc</span></span>
<span class="line">     */</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">up</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">createTable</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'post'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span></span>
<span class="line">            <span class="token string single-quoted-string">'id'</span> <span class="token operator">=></span> <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">primaryKey</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token string single-quoted-string">'title'</span> <span class="token operator">=></span> <span class="token variable">$this</span><span class="token operator">-></span><span class="token keyword type-declaration">string</span><span class="token punctuation">(</span><span class="token number">12</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">notNull</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">unique</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token string single-quoted-string">'body'</span> <span class="token operator">=></span> <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">text</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line">        <span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"></span>
<span class="line">    <span class="token doc-comment comment">/**</span>
<span class="line">     * <span class="token keyword">@inheritdoc</span></span>
<span class="line">     */</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">down</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">dropTable</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'post'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><blockquote>
<p>Note: 主键会被自动添加同时默认名称为 <code v-pre>id</code>。如果你想使用其他名称可以使用 <code v-pre>--fields=&quot;name:primaryKey&quot;</code> 来指定名称。</p>
</blockquote>
<h4 id="外键" tabindex="-1"><a class="header-anchor" href="#外键"><span>外键</span></a></h4>
<p>从 2.0.8 版本开始，生成器通过使用 <code v-pre>foreignKey</code> 关键字支持外键。</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">yii migrate/create create_post <span class="token parameter variable">--fields</span><span class="token operator">=</span><span class="token string">"author_id:integer:notNull:foreignKey(user),category_id:integer:defaultValue(1):foreignKey,title:string,body:text"</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>生成</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * Handles the creation for table `post`.</span>
<span class="line"> * Has foreign keys to the tables:</span>
<span class="line"> *</span>
<span class="line"> * - `user`</span>
<span class="line"> * - `category`</span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">class</span> <span class="token class-name-definition class-name">m160328_040430_create_post</span> <span class="token keyword">extends</span> <span class="token class-name">Migration</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token doc-comment comment">/**</span>
<span class="line">     * <span class="token keyword">@inheritdoc</span></span>
<span class="line">     */</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">up</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">createTable</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'post'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span></span>
<span class="line">            <span class="token string single-quoted-string">'id'</span> <span class="token operator">=></span> <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">primaryKey</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token string single-quoted-string">'author_id'</span> <span class="token operator">=></span> <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">integer</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">notNull</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token string single-quoted-string">'category_id'</span> <span class="token operator">=></span> <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">integer</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">defaultValue</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token string single-quoted-string">'title'</span> <span class="token operator">=></span> <span class="token variable">$this</span><span class="token operator">-></span><span class="token keyword type-declaration">string</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token string single-quoted-string">'body'</span> <span class="token operator">=></span> <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">text</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">        <span class="token comment">// creates index for column `author_id`</span></span>
<span class="line">        <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">createIndex</span><span class="token punctuation">(</span></span>
<span class="line">            <span class="token string single-quoted-string">'idx-post-author_id'</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token string single-quoted-string">'post'</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token string single-quoted-string">'author_id'</span></span>
<span class="line">        <span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">        <span class="token comment">// add foreign key for table `user`</span></span>
<span class="line">        <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">addForeignKey</span><span class="token punctuation">(</span></span>
<span class="line">            <span class="token string single-quoted-string">'fk-post-author_id'</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token string single-quoted-string">'post'</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token string single-quoted-string">'author_id'</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token string single-quoted-string">'user'</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token string single-quoted-string">'id'</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token string single-quoted-string">'CASCADE'</span></span>
<span class="line">        <span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">        <span class="token comment">// creates index for column `category_id`</span></span>
<span class="line">        <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">createIndex</span><span class="token punctuation">(</span></span>
<span class="line">            <span class="token string single-quoted-string">'idx-post-category_id'</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token string single-quoted-string">'post'</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token string single-quoted-string">'category_id'</span></span>
<span class="line">        <span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">        <span class="token comment">// add foreign key for table `category`</span></span>
<span class="line">        <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">addForeignKey</span><span class="token punctuation">(</span></span>
<span class="line">            <span class="token string single-quoted-string">'fk-post-category_id'</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token string single-quoted-string">'post'</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token string single-quoted-string">'category_id'</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token string single-quoted-string">'category'</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token string single-quoted-string">'id'</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token string single-quoted-string">'CASCADE'</span></span>
<span class="line">        <span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"></span>
<span class="line">    <span class="token doc-comment comment">/**</span>
<span class="line">     * <span class="token keyword">@inheritdoc</span></span>
<span class="line">     */</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">down</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token comment">// drops foreign key for table `user`</span></span>
<span class="line">        <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">dropForeignKey</span><span class="token punctuation">(</span></span>
<span class="line">            <span class="token string single-quoted-string">'fk-post-author_id'</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token string single-quoted-string">'post'</span></span>
<span class="line">        <span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">        <span class="token comment">// drops index for column `author_id`</span></span>
<span class="line">        <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">dropIndex</span><span class="token punctuation">(</span></span>
<span class="line">            <span class="token string single-quoted-string">'idx-post-author_id'</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token string single-quoted-string">'post'</span></span>
<span class="line">        <span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">        <span class="token comment">// drops foreign key for table `category`</span></span>
<span class="line">        <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">dropForeignKey</span><span class="token punctuation">(</span></span>
<span class="line">            <span class="token string single-quoted-string">'fk-post-category_id'</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token string single-quoted-string">'post'</span></span>
<span class="line">        <span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">        <span class="token comment">// drops index for column `category_id`</span></span>
<span class="line">        <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">dropIndex</span><span class="token punctuation">(</span></span>
<span class="line">            <span class="token string single-quoted-string">'idx-post-category_id'</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token string single-quoted-string">'post'</span></span>
<span class="line">        <span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">        <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">dropTable</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'post'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p><code v-pre>foreignKey</code> 关键字在字段描述中的位置不会影响生成的代码。 那意味着：</p>
<ul>
<li><code v-pre>author_id![:integer:](https://cdn.learnku.com/assets/images/emoji/integer.png)notNull:foreignKey(user)</code></li>
<li><code v-pre>author_id![:integer:](https://cdn.learnku.com/assets/images/emoji/integer.png)foreignKey(user):notNull</code></li>
<li><code v-pre>author_id:foreignKey(user)![:integer:](https://cdn.learnku.com/assets/images/emoji/integer.png)notNull</code></li>
</ul>
<p>以上都会生成相同的代码。</p>
<p><code v-pre>foreignKey</code> 关键字可以在圆括号中间接收一个参数， 这个参数将会成为生成外键所需的关联表的名称。 如果不传入任何参数，那么表名将会根据字段名来生成。</p>
<p>在上面的例子中，<code v-pre>author_id![:integer:](https://cdn.learnku.com/assets/images/emoji/integer.png)notNull:foreignKey(user)</code> 会生成一个 带有关联 <code v-pre>user</code> 表的外键，名称为 <code v-pre>author_id</code> 的字段， <code v-pre>category_id![:integer:](https://cdn.learnku.com/assets/images/emoji/integer.png)defaultValue(1):foreignKey</code> 会生成一个 带有关联 <code v-pre>category</code> 表的外键，名称为 <code v-pre>category_id</code> 的字段。</p>
<p>从 2.0.11 版本开始，<code v-pre>foreignKey</code> 接收第二个参数，跟第一个参数用空格分开。 这个参数表示生成的外键所关联字段的名称。 如果不传入第二个参数，字段名将从表模式（schema）中获得。 如果模式（schema）不存在，或者未设置主键，又或者是联合主键，字段将使用 <code v-pre>id</code> 作为默认名称。</p>
<h3 id="删除表" tabindex="-1"><a class="header-anchor" href="#删除表"><span>删除表</span></a></h3>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">yii migrate/create drop_post <span class="token parameter variable">--fields</span><span class="token operator">=</span><span class="token string">"title:string(12):notNull:unique,body:text"</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>生成</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">class</span> <span class="token class-name-definition class-name">m150811_220037_drop_post</span> <span class="token keyword">extends</span> <span class="token class-name">Migration</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">up</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">dropTable</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'post'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">down</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">createTable</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'post'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span></span>
<span class="line">            <span class="token string single-quoted-string">'id'</span> <span class="token operator">=></span> <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">primaryKey</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token string single-quoted-string">'title'</span> <span class="token operator">=></span> <span class="token variable">$this</span><span class="token operator">-></span><span class="token keyword type-declaration">string</span><span class="token punctuation">(</span><span class="token number">12</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">notNull</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">unique</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token string single-quoted-string">'body'</span> <span class="token operator">=></span> <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">text</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line">        <span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="添加字段" tabindex="-1"><a class="header-anchor" href="#添加字段"><span>添加字段</span></a></h3>
<p>如果迁移的名称遵循 <code v-pre>add_xxx_to_yyy</code> 这样的格式， 生成的类文件将会包含必要的 <code v-pre>addColumn</code> 和 <code v-pre>dropColumn</code>。</p>
<p>添加字段：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">yii migrate/create add_position_to_post <span class="token parameter variable">--fields</span><span class="token operator">=</span><span class="token string">"position:integer"</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>生成</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">class</span> <span class="token class-name-definition class-name">m150811_220037_add_position_to_post</span> <span class="token keyword">extends</span> <span class="token class-name">Migration</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">up</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">addColumn</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'post'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'position'</span><span class="token punctuation">,</span> <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">integer</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">down</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">dropColumn</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'post'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'position'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>你可以像如下这样指定多个字段：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">yii migrate/create add_xxx_column_yyy_column_to_zzz_table <span class="token parameter variable">--fields</span><span class="token operator">=</span><span class="token string">"xxx:integer,yyy:text"</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h3 id="删除字段" tabindex="-1"><a class="header-anchor" href="#删除字段"><span>删除字段</span></a></h3>
<p>如果迁移的名称遵循 <code v-pre>drop_xxx_from_yyy</code> 这样的格式， 生成的类文件将会包含必要的 <code v-pre>addColumn</code> 和 <code v-pre>dropColumn</code>。</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">yii migrate/create drop_position_from_post <span class="token parameter variable">--fields</span><span class="token operator">=</span><span class="token string">"position:integer"</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>生成</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">class</span> <span class="token class-name-definition class-name">m150811_220037_drop_position_from_post</span> <span class="token keyword">extends</span> <span class="token class-name">Migration</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">up</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">dropColumn</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'post'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'position'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">down</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">addColumn</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'post'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'position'</span><span class="token punctuation">,</span> <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">integer</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="添加连接表" tabindex="-1"><a class="header-anchor" href="#添加连接表"><span>添加连接表</span></a></h3>
<p>如果迁移的名称遵循 <code v-pre>create_junction_xxx_and_yyy</code> 这样的格式，创建连接表的必要代码将会被生成。</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">yii migrate/create create_junction_post_and_tag <span class="token parameter variable">--fields</span><span class="token operator">=</span><span class="token string">"created_at:dateTime"</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>生成</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * Handles the creation for table `post_tag`.</span>
<span class="line"> * Has foreign keys to the tables:</span>
<span class="line"> *</span>
<span class="line"> * - `post`</span>
<span class="line"> * - `tag`</span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">class</span> <span class="token class-name-definition class-name">m160328_041642_create_junction_post_and_tag</span> <span class="token keyword">extends</span> <span class="token class-name">Migration</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token doc-comment comment">/**</span>
<span class="line">     * <span class="token keyword">@inheritdoc</span></span>
<span class="line">     */</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">up</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">createTable</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'post_tag'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span></span>
<span class="line">            <span class="token string single-quoted-string">'post_id'</span> <span class="token operator">=></span> <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">integer</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token string single-quoted-string">'tag_id'</span> <span class="token operator">=></span> <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">integer</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token string single-quoted-string">'created_at'</span> <span class="token operator">=></span> <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">dateTime</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token string single-quoted-string">'PRIMARY KEY(post_id, tag_id)'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">        <span class="token comment">// creates index for column `post_id`</span></span>
<span class="line">        <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">createIndex</span><span class="token punctuation">(</span></span>
<span class="line">            <span class="token string single-quoted-string">'idx-post_tag-post_id'</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token string single-quoted-string">'post_tag'</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token string single-quoted-string">'post_id'</span></span>
<span class="line">        <span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">        <span class="token comment">// add foreign key for table `post`</span></span>
<span class="line">        <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">addForeignKey</span><span class="token punctuation">(</span></span>
<span class="line">            <span class="token string single-quoted-string">'fk-post_tag-post_id'</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token string single-quoted-string">'post_tag'</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token string single-quoted-string">'post_id'</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token string single-quoted-string">'post'</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token string single-quoted-string">'id'</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token string single-quoted-string">'CASCADE'</span></span>
<span class="line">        <span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">        <span class="token comment">// creates index for column `tag_id`</span></span>
<span class="line">        <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">createIndex</span><span class="token punctuation">(</span></span>
<span class="line">            <span class="token string single-quoted-string">'idx-post_tag-tag_id'</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token string single-quoted-string">'post_tag'</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token string single-quoted-string">'tag_id'</span></span>
<span class="line">        <span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">        <span class="token comment">// add foreign key for table `tag`</span></span>
<span class="line">        <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">addForeignKey</span><span class="token punctuation">(</span></span>
<span class="line">            <span class="token string single-quoted-string">'fk-post_tag-tag_id'</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token string single-quoted-string">'post_tag'</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token string single-quoted-string">'tag_id'</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token string single-quoted-string">'tag'</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token string single-quoted-string">'id'</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token string single-quoted-string">'CASCADE'</span></span>
<span class="line">        <span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"></span>
<span class="line">    <span class="token doc-comment comment">/**</span>
<span class="line">     * <span class="token keyword">@inheritdoc</span></span>
<span class="line">     */</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">down</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token comment">// drops foreign key for table `post`</span></span>
<span class="line">        <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">dropForeignKey</span><span class="token punctuation">(</span></span>
<span class="line">            <span class="token string single-quoted-string">'fk-post_tag-post_id'</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token string single-quoted-string">'post_tag'</span></span>
<span class="line">        <span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">        <span class="token comment">// drops index for column `post_id`</span></span>
<span class="line">        <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">dropIndex</span><span class="token punctuation">(</span></span>
<span class="line">            <span class="token string single-quoted-string">'idx-post_tag-post_id'</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token string single-quoted-string">'post_tag'</span></span>
<span class="line">        <span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">        <span class="token comment">// drops foreign key for table `tag`</span></span>
<span class="line">        <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">dropForeignKey</span><span class="token punctuation">(</span></span>
<span class="line">            <span class="token string single-quoted-string">'fk-post_tag-tag_id'</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token string single-quoted-string">'post_tag'</span></span>
<span class="line">        <span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">        <span class="token comment">// drops index for column `tag_id`</span></span>
<span class="line">        <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">dropIndex</span><span class="token punctuation">(</span></span>
<span class="line">            <span class="token string single-quoted-string">'idx-post_tag-tag_id'</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token string single-quoted-string">'post_tag'</span></span>
<span class="line">        <span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">        <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">dropTable</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'post_tag'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>从 2.0.11 版本开始，连接表的外键字段名将从表模式（schema）中获得。如果模式（schema）不存在，或者未设置主键，又或者是联合主键，字段将使用 <code v-pre>id</code> 作为默认名称。</p>
<h3 id="事务迁移" tabindex="-1"><a class="header-anchor" href="#事务迁移"><span>事务迁移</span></a></h3>
<p>当需要实现复杂的数据库迁移的时候，确定每一个迁移的执行是否成功或失败就变得相当重要了， 因为这将影响到数据库的完整性和一致性。为了达到这个目标，我们建议你把每个迁移里面的 数据库操作都封装到一个 <a href="https://github.com/yiisoft/yii2/blob/master/docs/guide-zh-CN/db-dao.md#performing-transactions" target="_blank" rel="noopener noreferrer">执行事务</a> 里面。</p>
<p>实现事务迁移的一个更为简便的方法是把迁移的代码都放到 <code v-pre>safeUp()</code> 和 <code v-pre>safeDown()</code> 方法里面。 它们与 <code v-pre>up()</code> 和 <code v-pre>down()</code> 的不同点就在于它们是被隐式的封装到事务当中的。如此一来，只要这些方法里面的任何一个操作失败了，那么所有之前的操作都会被自动的回滚。</p>
<p>在如下的例子当中，除了创建 <code v-pre>news</code> 表以外，我们还插入了一行初始化数据到表里面。</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">yii<span class="token punctuation">\</span>db<span class="token punctuation">\</span>Schema</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">yii<span class="token punctuation">\</span>db<span class="token punctuation">\</span>Migration</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">class</span> <span class="token class-name-definition class-name">m150101_185401_create_news_table</span> <span class="token keyword">extends</span> <span class="token class-name">Migration</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">safeUp</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">createTable</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'news'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span></span>
<span class="line">            <span class="token string single-quoted-string">'id'</span> <span class="token operator">=></span> <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">primaryKey</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token string single-quoted-string">'title'</span> <span class="token operator">=></span> <span class="token variable">$this</span><span class="token operator">-></span><span class="token keyword type-declaration">string</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">notNull</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token string single-quoted-string">'content'</span> <span class="token operator">=></span> <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">text</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">        <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">insert</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'news'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span></span>
<span class="line">            <span class="token string single-quoted-string">'title'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'test 1'</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token string single-quoted-string">'content'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'content 1'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">safeDown</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">delete</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'news'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'id'</span> <span class="token operator">=></span> <span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">        <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">dropTable</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'news'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>需要注意的是，当你在 <code v-pre>safeUp()</code> 当中执行多个数据库操作的时候，你应该在 <code v-pre>safeDown()</code> 方法当中反转它们的执行顺序。 在上面的例子当中，我们在 <code v-pre>safeUp()</code> 方法当中首先创建了一张表，然后插入了一条数据；而在 <code v-pre>safeDown()</code> 方法当中，我们首先删除那一行数据，然后才删除那张表。</p>
<blockquote>
<p>Note: 并不是所有的数据库都支持事务。有些数据库查询也是不能被放到事务里面的。 在 <a href="http://dev.mysql.com/doc/refman/5.7/en/implicit-commit.html" target="_blank" rel="noopener noreferrer">隐式提交</a> 章节当中有相关的例子可以参考。 如果遇到这种情况的话，那么你应该使用 <code v-pre>up()</code> 和 <code v-pre>down()</code> 方法进行替代。</p>
</blockquote>
<h3 id="访问数据库的方法" tabindex="-1"><a class="header-anchor" href="#访问数据库的方法"><span>访问数据库的方法</span></a></h3>
<p>迁移的基类 <code v-pre>yii\db\Migration</code> 提供了一整套访问和操作数据库的方法。你可能会发现这些方法的命名和 <code v-pre>yii\db\Command</code> 类提供的 <a href="https://github.com/yiisoft/yii2/blob/master/docs/guide-zh-CN/db-dao.md" target="_blank" rel="noopener noreferrer">DAO 方法</a> 很类似。 例如，[[yii\db\Migration::createTable()]] 方法可以创建一张新的表，这和 [[yii\db\Command::createTable()]] 的功能是一模一样的。</p>
<p>使用 <code v-pre>yii\db\Migration</code> 所提供的方法的好处在于你不需要再显式的创建 <code v-pre>yii\db\Command</code> 实例，而且在执行每个方法的时候都会显示一些有用的信息来告诉我们数据库操作是不是都已经完成，还有它们完成这些操作花了多长时间等等。</p>
<p>如下是所有这些数据库访问方法的列表：</p>
<ul>
<li>[[yii\db\Migration::execute()|execute()]]：执行一条 SQL 语句</li>
<li>[[yii\db\Migration::insert()|insert()]]：插入单行数据</li>
<li>[[yii\db\Migration::batchInsert()|batchInsert()]]：插入多行数据</li>
<li>[[yii\db\Migration::update()|update()]]：更新数据</li>
<li>[[yii\db\Migration::delete()|delete()]]：删除数据</li>
<li>[[yii\db\Migration::createTable()|createTable()]]：创建表</li>
<li>[[yii\db\Migration::renameTable()|renameTable()]]：重命名表名</li>
<li>[[yii\db\Migration::dropTable()|dropTable()]]：删除一张表</li>
<li>[[yii\db\Migration::truncateTable()|truncateTable()]]：清空表中的所有数据</li>
<li>[[yii\db\Migration::addColumn()|addColumn()]]：加一个字段</li>
<li>[[yii\db\Migration::renameColumn()|renameColumn()]]：重命名字段名称</li>
<li>[[yii\db\Migration::dropColumn()|dropColumn()]]：删除一个字段</li>
<li>[[yii\db\Migration::alterColumn()|alterColumn()]]：修改字段</li>
<li>[[yii\db\Migration::addPrimaryKey()|addPrimaryKey()]]：添加一个主键</li>
<li>[[yii\db\Migration::dropPrimaryKey()|dropPrimaryKey()]]：删除一个主键</li>
<li>[[yii\db\Migration::addForeignKey()|addForeignKey()]]：添加一个外键</li>
<li>[[yii\db\Migration::dropForeignKey()|dropForeignKey()]]：删除一个外键</li>
<li>[[yii\db\Migration::createIndex()|createIndex()]]：创建一个索引</li>
<li>[[yii\db\Migration::dropIndex()|dropIndex()]]：删除一个索引</li>
<li>[[yii\db\Migration::addCommentOnColumn()|addCommentOnColumn()]]：添加字段的注释</li>
<li>[[yii\db\Migration::dropCommentFromColumn()|dropCommentFromColumn()]]：删除字段的注释</li>
<li>[[yii\db\Migration::addCommentOnTable()|addCommentOnTable()]]：添加表的注释</li>
<li>[[yii\db\Migration::dropCommentFromTable()|dropCommentFromTable()]]：删除表的注释</li>
</ul>
<blockquote>
<p>Tip: <code v-pre>yii\db\Migration</code> 并没有提供数据库的查询方法。 这是因为通常你是不需要去数据库把数据一行一行查出来再显示出来的。 另外一个原因是你完全可以使用强大的 <a href="https://github.com/yiisoft/yii2/blob/master/docs/guide-zh-CN/db-query-builder.md" target="_blank" rel="noopener noreferrer">Query Builder 查询构建器</a> 来构建和查询。<br>
你可以像这样在迁移中使用查询构建器：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token comment">// 更新所有用户的 status 字段</span></span>
<span class="line"><span class="token keyword">foreach</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">Query</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">from</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'users'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">each</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token keyword">as</span> <span class="token variable">$user</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">update</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'users'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'status'</span> <span class="token operator">=></span> <span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'id'</span> <span class="token operator">=></span> <span class="token variable">$user</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'id'</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div></blockquote>
<h2 id="提交迁移" tabindex="-1"><a class="header-anchor" href="#提交迁移"><span>提交迁移</span></a></h2>
<p>为了将数据库升级到最新的结构，你应该使用如下命令来提交所有新的迁移：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">yii migrate</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>这条命令会列出迄今为止所有未提交的迁移。如果你确定你需要提交这些迁移，它将会按照类名当中的时间戳的顺序，一个接着一个的运行每个新的迁移类里面的 <code v-pre>up()</code> 或者是 <code v-pre>safeUp()</code> 方法。 如果其中任意一个迁移提交失败了，那么这条命令将会退出并停止剩下的那些还未执行的迁移。</p>
<blockquote>
<p>Tip: 如果你的服务器没有命令行， 你可以尝试 <a href="https://github.com/samdark/yii2-webshell" target="_blank" rel="noopener noreferrer">web shell</a> 这个扩展。</p>
</blockquote>
<p>对于每一个成功提交的迁移，这条命令都会在一个叫做 <code v-pre>migration</code> 的数据库表中插入一条包含应用程序成功提交迁移的记录，该记录将帮助迁移工具判断哪些迁移已经提交，哪些还没有提交。</p>
<blockquote>
<p>Info: 迁移工具将会自动在数据库当中创建 <code v-pre>migration</code> 表，该数据库是在该命令的 [[yii\console\controllers\MigrateController::db]] 选项当中指定的。默认情况下，是由 <code v-pre>db</code> <a href="https://github.com/yiisoft/yii2/blob/master/docs/guide-zh-CN/structure-application-components.md" target="_blank" rel="noopener noreferrer">应用组件</a> 指定的。</p>
</blockquote>
<p>有时，你可能只需要提交一个或者少数的几个迁移，你可以使用该命令指定需要执行的条数，而不是执行所有的可用迁移。例如，如下命令将会尝试提交前三个可用的迁移：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">yii migrate <span class="token number">3</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>你也可以指定一个特定的迁移，按照如下格式使用 <code v-pre>migrate/to</code> 命令来指定数据库应该提交哪一个迁移：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">yii migrate/to 150101_185401                      <span class="token comment"># using timestamp to specify the migration 使用时间戳来指定迁移</span></span>
<span class="line">yii migrate/to <span class="token string">"2015-01-01 18:54:01"</span>              <span class="token comment"># using a string that can be parsed by strtotime() 使用一个可以被 strtotime() 解析的字符串</span></span>
<span class="line">yii migrate/to m150101_185401_create_news_table   <span class="token comment"># using full name 使用全名</span></span>
<span class="line">yii migrate/to <span class="token number">1392853618</span>                         <span class="token comment"># using UNIX timestamp 使用 UNIX 时间戳</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>如果在指定要提交的迁移前面还有未提交的迁移，那么在执行这个被指定的迁移之前，这些还未提交的迁移会先被提交。</p>
<p>如果被指定提交的迁移在之前已经被提交过，那么在其之后的那些迁移将会被还原。</p>
<h2 id="还原迁移" tabindex="-1"><a class="header-anchor" href="#还原迁移"><span>还原迁移</span></a></h2>
<p>你可以使用如下命令来还原其中一个或多个意见被提交过的迁移：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">yii migrate<span class="token operator">/</span>down     <span class="token comment"># revert the most recently applied migration 还原最近一次提交的迁移</span></span>
<span class="line">yii migrate<span class="token operator">/</span>down <span class="token number">3</span>   <span class="token comment"># revert the most 3 recently applied migrations 还原最近三次提交的迁移</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div></div></div><blockquote>
<p>Note: 并不是所有的迁移都能被还原。 尝试还原这类迁移将可能导致报错甚至是终止所有的还原进程。</p>
</blockquote>
<h2 id="重做迁移" tabindex="-1"><a class="header-anchor" href="#重做迁移"><span>重做迁移</span></a></h2>
<p>重做迁移的意思是先还原指定的迁移，然后再次提交。 如下所示：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">yii migrate/redo        <span class="token comment"># 重做最近一次提交的迁移</span></span>
<span class="line">yii migrate/redo <span class="token number">3</span>      <span class="token comment"># 重做最近三次提交的迁移</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div></div></div><blockquote>
<p>Note: 如果一个迁移是不能被还原的，那么你将无法对它进行重做。</p>
</blockquote>
<h2 id="刷新迁移" tabindex="-1"><a class="header-anchor" href="#刷新迁移"><span>刷新迁移</span></a></h2>
<p>从 2.0.13 版本开始，你可以从数据库中删除所有的表和外键，从头开始重新提交所有迁移。</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">yii migrate/fresh       <span class="token comment"># 清空数据库并从头开始应用所有迁移。</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h2 id="列出迁移" tabindex="-1"><a class="header-anchor" href="#列出迁移"><span>列出迁移</span></a></h2>
<p>你可以使用如下命令列出那些提交了的或者是还未提交的迁移：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">yii migrate/history     <span class="token comment"># 显示最近10次提交的迁移</span></span>
<span class="line">yii migrate/history <span class="token number">5</span>   <span class="token comment"># 显示最近5次提交的迁移</span></span>
<span class="line">yii migrate/history all <span class="token comment"># 显示所有已经提交过的迁移</span></span>
<span class="line"></span>
<span class="line">yii migrate/new         <span class="token comment"># 显示前10个还未提交的迁移</span></span>
<span class="line">yii migrate/new <span class="token number">5</span>       <span class="token comment"># 显示前5个还未提交的迁移</span></span>
<span class="line">yii migrate/new all     <span class="token comment"># 显示所有还未提交的迁移</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="修改迁移历史" tabindex="-1"><a class="header-anchor" href="#修改迁移历史"><span>修改迁移历史</span></a></h2>
<p>有时候你也许需要简单的标记一下你的数据库已经升级到一个特定的迁移，而不是实际提交或者是还原迁移。 这个经常会发生在你手动的改变数据库的一个特定状态，而又不想相应的迁移被重复提交。 那么你可以使用如下命令来达到目的：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">yii migrate/mark 150101_185401                      <span class="token comment"># 使用时间戳来指定迁移</span></span>
<span class="line">yii migrate/mark <span class="token string">"2015-01-01 18:54:01"</span>              <span class="token comment"># 使用一个可以被 strtotime() 解析的字符串</span></span>
<span class="line">yii migrate/mark m150101_185401_create_news_table   <span class="token comment"># 使用全名</span></span>
<span class="line">yii migrate/mark <span class="token number">1392853618</span>                         <span class="token comment"># 使用 UNIX 时间戳</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>该命令将会添加或者删除 <code v-pre>migration</code> 表当中的某几行数据来表明数据库已经提交到了指定的某个迁移上。执行这条命令期间不会有任何的迁移会被提交或还原。</p>
<h2 id="自定义迁移" tabindex="-1"><a class="header-anchor" href="#自定义迁移"><span>自定义迁移</span></a></h2>
<p>有很多方法可以自定义迁移命令。</p>
<h3 id="使用命令行选项" tabindex="-1"><a class="header-anchor" href="#使用命令行选项"><span>使用命令行选项</span></a></h3>
<p>迁移命令附带了几个命令行选项，可以用来自定义它的行为：</p>
<ul>
<li>
<p><code v-pre>interactive</code>：boolean (默认值为 true)，指定是否以交互模式来运行迁移。 当被设置为 true 时，在命令执行某些操作前，会提示用户。如果你希望在后台执行该命令， 那么你应该把它设置成 false。</p>
</li>
<li>
<p><code v-pre>migrationPath</code>：string|array (默认值为 <code v-pre>@app/migrations</code>)，指定存放所有迁移类文件的目录。该选项可以是一个目录的路径， 也可以是 <a href="https://github.com/yiisoft/yii2/blob/master/docs/guide-zh-CN/concept-aliases.md" target="_blank" rel="noopener noreferrer">路径别名</a>。需要注意的是指定的目录必选存在， 否则将会触发一个错误。 从 2.0.12 版本开始，可以用一个数组来指定从多个来源读取迁移类文件。</p>
</li>
<li>
<p><code v-pre>migrationTable</code>：string (默认值为 <code v-pre>migration</code>)，指定用于存储迁移历史信息的数据库表名称。 如果这张表不存在，那么迁移命令将自动创建这张表。当然你也可以使用这样的字段结构： <code v-pre>version varchar(255) primary key, apply_time integer</code> 来手动创建这张表。</p>
</li>
<li>
<p><code v-pre>db</code>：string (默认值为 <code v-pre>db</code>)，指定数据库 <a href="https://github.com/yiisoft/yii2/blob/master/docs/guide-zh-CN/structure-application-components.md" target="_blank" rel="noopener noreferrer">application component</a> 的 ID。 它指的是将会被该命令迁移的数据库。</p>
</li>
<li>
<p><code v-pre>templateFile</code>：string (默认值为 <code v-pre>@yii/views/migration.php</code>)， 指定生产迁移框架代码类文件的模版文件路径。 该选项即可以使用文件路径来指定，也可以使用路径 <a href="https://github.com/yiisoft/yii2/blob/master/docs/guide-zh-CN/concept-aliases.md" target="_blank" rel="noopener noreferrer">别名</a> 来指定。 该模版文件是一个可以使用预定义变量 <code v-pre>$className</code> 来获取迁移类名称的 PHP 脚本。</p>
</li>
<li>
<p><code v-pre>generatorTemplateFiles</code>：array (defaults to <code v-pre>[ 'create_table' =&gt; '@yii/views/createTableMigration.php', 'drop_table' =&gt; '@yii/views/dropTableMigration.php', 'add_column' =&gt; '@yii/views/addColumnMigration.php', 'drop_column' =&gt; '@yii/views/dropColumnMigration.php', 'create_junction' =&gt; '@yii/views/createJunctionMigration.php' ]</code>)，指定生成迁移代码的模版文件，查看 “<a href="https://github.com/yiisoft/yii2/blob/master/docs/guide-zh-CN/db-migrations.md#generating-migrations" target="_blank" rel="noopener noreferrer">Generating Migrations</a>“ 了解更多细节。</p>
</li>
<li>
<p><code v-pre>fields</code>：由用来创建迁移代码的多个字段定义字符串所组成的数组。默认是 <code v-pre>[]</code>。 字段定义的格式是 <code v-pre>COLUMN_NAME![:COLUMN_TYPE:](https://cdn.learnku.com/assets/images/emoji/COLUMN_TYPE.png)COLUMN_DECORATOR</code>。例如，<code v-pre>--fields=name:string(12):notNull</code> 会创建 一个长度为 12 的，非空的，字符串类型的字段。</p>
</li>
</ul>
<p>如下例子向我们展示了如何使用这些选项：</p>
<p>例如，如果我们需要迁移一个 <code v-pre>forum</code> 模块， 而该迁移文件放在该模块下的 <code v-pre>migrations</code> 目录当中， 那么我们可以使用如下命令：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line"><span class="token comment"># 在 forum 模块中以非交互模式进行迁移</span></span>
<span class="line">yii migrate <span class="token parameter variable">--migrationPath</span><span class="token operator">=</span>@app/modules/forum/migrations <span class="token parameter variable">--interactive</span><span class="token operator">=</span><span class="token number">0</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="全局配置命令" tabindex="-1"><a class="header-anchor" href="#全局配置命令"><span>全局配置命令</span></a></h3>
<p>为了避免运行迁移命令的时候每次都要重复的输入一些同样的参数， 你可以选择在应用程序配置当中进行全局配置，一劳永逸：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">return</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'controllerMap'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'migrate'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">            <span class="token string single-quoted-string">'class'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'yii\console\controllers\MigrateController'</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token string single-quoted-string">'migrationTable'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'backend_migration'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>如上所示配置，在每次运行迁移命令的时候，<code v-pre>backend_migration</code> 表将会被用来记录迁移历史。 你再也不需要通过 <code v-pre>migrationTable</code> 命令行参数来指定这张历史纪录表了。</p>
<h3 id="使用命名空间的迁移" tabindex="-1"><a class="header-anchor" href="#使用命名空间的迁移"><span>使用命名空间的迁移</span></a></h3>
<p>自从 2.0.10 版本开始，你可以为迁移类使用命名空间。你可以通过 [[yii\console\controllers\MigrateController::migrationNamespaces]] 来指定迁移会用到的命名空间。使用命名空间将允许你利用多个源的位置进行迁移。例如：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">return</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'controllerMap'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'migrate'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">            <span class="token string single-quoted-string">'class'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'yii\console\controllers\MigrateController'</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token string single-quoted-string">'migrationPath'</span> <span class="token operator">=></span> <span class="token constant">null</span><span class="token punctuation">,</span> <span class="token comment">// disable non-namespaced migrations if app\migrations is listed below</span></span>
<span class="line">            <span class="token string single-quoted-string">'migrationNamespaces'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">                <span class="token string single-quoted-string">'app\migrations'</span><span class="token punctuation">,</span> <span class="token comment">// Common migrations for the whole application</span></span>
<span class="line">                <span class="token string single-quoted-string">'module\migrations'</span><span class="token punctuation">,</span> <span class="token comment">// Migrations for the specific project's module</span></span>
<span class="line">                <span class="token string single-quoted-string">'some\extension\migrations'</span><span class="token punctuation">,</span> <span class="token comment">// Migrations for the specific extension</span></span>
<span class="line">            <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><blockquote>
<p>Note: 从不同命名空间提交的迁移会创建出一份<strong>单一的</strong>迁移历史，比如你不能只从某个特定的命名空间去提交或者还原迁移。</p>
</blockquote>
<p>当你正在操作使用命名空间的迁移时：比如创建迁移，还原迁移等，你应当在迁移名称前指明完整的命名空间。要注意反斜线(<code v-pre>\</code>)在 shell 中会被当成特殊字符，你应该对反斜线进行编码，避免 shell 报错或者产生不正确的结果。例如：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">yii migrate/create <span class="token string">'app\\migrations\\createUserTable'</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><blockquote>
<p>Note: 通过 [[yii\console\controllers\MigrateController::migrationPath]] 声明的迁移不能包含命名空间，使用命名空间的迁移只能通过 [[yii\console\controllers\MigrateController::migrationNamespaces]] 的属性来提交。</p>
</blockquote>
<p>从版本 2.0.12 开始，[[yii\console\controllers\MigrateController::migrationPath]] 属性也接收一个数组作为参数, 这个数组指明了没有使用命名空间的多个迁移的目录。这个参数主要用于已经从多个位置进行了迁移的现有项目。这些迁移主要来自外部资源，比如其他开发人员开发的 Yii 扩展，当使用新方法时，这些迁移很难改成使用命名空间。</p>
<h3 id="分离的迁移" tabindex="-1"><a class="header-anchor" href="#分离的迁移"><span>分离的迁移</span></a></h3>
<p>有时候我们并不想整个项目所有的迁移都记录到同一份迁移历史中。例如：你可能安装了 ‘blog’ 扩展，它有自己独立的功能和迁移，不会影响到用于项目主要功能的其他扩展。</p>
<p>如果你想完全分开提交和追踪多个迁移，你可以同时配置使用不同命名空间和历史记录表的多条迁移命令：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">return</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'controllerMap'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token comment">// Common migrations for the whole application</span></span>
<span class="line">        <span class="token string single-quoted-string">'migrate-app'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">            <span class="token string single-quoted-string">'class'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'yii\console\controllers\MigrateController'</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token string single-quoted-string">'migrationNamespaces'</span> <span class="token operator">=></span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'app\migrations'</span><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token string single-quoted-string">'migrationTable'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'migration_app'</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token string single-quoted-string">'migrationPath'</span> <span class="token operator">=></span> <span class="token constant">null</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token comment">// Migrations for the specific project's module</span></span>
<span class="line">        <span class="token string single-quoted-string">'migrate-module'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">            <span class="token string single-quoted-string">'class'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'yii\console\controllers\MigrateController'</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token string single-quoted-string">'migrationNamespaces'</span> <span class="token operator">=></span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'module\migrations'</span><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token string single-quoted-string">'migrationTable'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'migration_module'</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token string single-quoted-string">'migrationPath'</span> <span class="token operator">=></span> <span class="token constant">null</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token comment">// Migrations for the specific extension</span></span>
<span class="line">        <span class="token string single-quoted-string">'migrate-rbac'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">            <span class="token string single-quoted-string">'class'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'yii\console\controllers\MigrateController'</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token string single-quoted-string">'migrationPath'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'@yii/rbac/migrations'</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token string single-quoted-string">'migrationTable'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'migration_rbac'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>请注意，要同步数据库，您现在需要运行多个命令而不是一个：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">yii migrate-app</span>
<span class="line">yii migrate-module</span>
<span class="line">yii migrate-rbac</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="迁移多个数据库" tabindex="-1"><a class="header-anchor" href="#迁移多个数据库"><span>迁移多个数据库</span></a></h2>
<p>默认情况下，迁移将会提交到由 <code v-pre>db</code> <a href="https://github.com/yiisoft/yii2/blob/master/docs/guide-zh-CN/structure-application-components.md" target="_blank" rel="noopener noreferrer">应用组件</a> 所定义的同一个数据库当中。如果你需要提交到不同的数据库，你可以像下面那样指定 <code v-pre>db</code> 命令行选项，</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">yii migrate <span class="token parameter variable">--db</span><span class="token operator">=</span>db2</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>上面的命令将会把迁移提交到 <code v-pre>db2</code> 数据库当中。</p>
<p>有些时候你需要提交 <em>一些</em> 迁移到一个数据库，而另外一些则提交到另一个数据库。为了达到这个目的，你应该在实现一个迁移类的时候指定需要用到的数据库组件的 ID ，如下所示：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token php language-php"><span class="token delimiter important">&lt;?php</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">yii<span class="token punctuation">\</span>db<span class="token punctuation">\</span>Migration</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">class</span> <span class="token class-name-definition class-name">m150101_185401_create_news_table</span> <span class="token keyword">extends</span> <span class="token class-name">Migration</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">init</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token variable">$this</span><span class="token operator">-></span><span class="token property">db</span> <span class="token operator">=</span> <span class="token string single-quoted-string">'db2'</span><span class="token punctuation">;</span></span>
<span class="line">        <span class="token keyword static-context">parent</span><span class="token operator">::</span><span class="token function">init</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>即使你使用 <code v-pre>db</code> 命令行选项指定了另外一个不同的数据库，上面的迁移还是会被提交到 <code v-pre>db2</code> 当中。需要注意的是这个时候迁移的历史信息依然会被记录到 <code v-pre>db</code> 命令行选项所指定的数据库当中。</p>
<p>如果有多个迁移都使用到了同一个数据库，那么建议你创建一个迁移的基类，里面包含上述的 <code v-pre>init()</code> 代码。然后每个迁移类都继承这个基类就可以了。</p>
<blockquote>
<p>Tip: 除了在 [[yii\db\Migration::db]] 参数当中进行设置以外，你还可以通过在迁移类中创建新的数据库连接来操作不同的数据库。然后通过这些连接再使用 <a href="https://github.com/yiisoft/yii2/blob/master/docs/guide-zh-CN/db-dao.md" target="_blank" rel="noopener noreferrer">DAO 方法</a> 来操作不同的数据库。</p>
</blockquote>
<p>另外一个可以让你迁移多个数据库的策略是把迁移存放到不同的目录下，然后你可以通过如下命令分别对不同的数据库进行迁移：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">yii migrate <span class="token parameter variable">--migrationPath</span><span class="token operator">=</span>@app/migrations/db1 <span class="token parameter variable">--db</span><span class="token operator">=</span>db1</span>
<span class="line">yii migrate <span class="token parameter variable">--migrationPath</span><span class="token operator">=</span>@app/migrations/db2 <span class="token parameter variable">--db</span><span class="token operator">=</span>db2</span>
<span class="line"><span class="token punctuation">..</span>.</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>第一条命令将会把 <code v-pre>@app/migrations/db1</code> 目录下的迁移提交到 <code v-pre>db1</code> 数据库当中，第二条命令则会把 <code v-pre>@app/migrations/db2</code> 下的迁移提交到 <code v-pre>db2</code> 数据库当中，以此类推。</p>
<blockquote>
<p>💖喜欢本文档的，欢迎点赞、收藏、留言或转发，谢谢支持！<br>
作者邮箱：zhuzixian520@126.com，github地址：<a href="https://github.com/zhuzixian520" target="_blank" rel="noopener noreferrer">github.com/zhuzixian520</a></p>
</blockquote>
</div></template>


